﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneBusinessList : UserControl
    {
        private VodafoneController _vodafoneController;
        private Controller _globalController;
        
        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                _vodafoneController.BusinessSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
                clientesBindingSource.DataSource = _vodafoneController.Clients;
            }
        }

        public Controller GlobalController
        {
            set
            {
                _globalController = value;
                entityRepositoryItemLookUpEdit.DataSource = _globalController.Entities;
            }
        }

        #endregion

        public VodafoneBusinessList()
        {
            InitializeComponent();
            editToolStripMenuItem.Click += editToolStripMenuItem_Click;
            addToolStripMenuItem.Click += addToolStripMenuItem_Click;
            gridView.DoubleClick += gridView_DoubleClick;
            contextMenuStrip.Opening += contextMenuStrip_Opening;
        }
        
        #region Private Methods

        private void SetGridDataSource()
        {
            gridControl.DataSource = _vodafoneController.Business;
        }

        private static Negocio GetClone(Negocio dealerSetup)
        {
            Negocio document = new Negocio();
            document.IdEntidade = dealerSetup.IdEntidade;
            document.IdCliente = dealerSetup.IdCliente;
            document.NIFNovo = dealerSetup.NIFNovo;
            return document;
        }

        private static void Merge(Negocio business, Negocio document)
        {
            business.IdEntidade = document.IdEntidade;
            business.IdCliente = document.IdCliente;
            business.NIFNovo = document.NIFNovo;
        }

        private void ShowBusiness(Negocio business)
        {
            Negocio document = GetClone(business);
            VodafoneBusiness window = new VodafoneBusiness();
            window.VodafoneController = _vodafoneController;
            window.GlobalController = _globalController;
            window.Document = document;
            if (DialogResult.OK != window.ShowDialog())
                return;
            Merge(business, window.Document);
            _vodafoneController.SaveBusiness(business);
        }

        #endregion

        void gridView_DoubleClick(object sender, EventArgs e)
        {
            Negocio business = (Negocio)gridView.GetRow(gridView.FocusedRowHandle);
            if (business == null)
                return;
            ShowBusiness(business);
        }

        void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Negocio business = (Negocio)gridView.GetRow(gridView.FocusedRowHandle);
            ShowBusiness(business);
        }

        void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Negocio business = new Negocio();
            business.IdNegocio = -1;
            business.NIFNovo = false;
            ShowBusiness(business);
        }

        void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            Negocio business = (Negocio)gridView.GetRow(gridView.FocusedRowHandle);
            editToolStripMenuItem.Enabled = business != null;
        } 

    }
}
